<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */

class Gio_Core_Array
{
    /**
     * The main function for converting to an XML document.
     * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
     *
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be - defaultsto data.
     * @param SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     */
    public static function toXml($data, $rootNodeName = 'data', &$xml = null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (@ini_get('zend.ze1_compatibility_mode') == 1) {
           @ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if (is_null($xml)) {
            $xml = @simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
        }

        // loop through the data passed in.
        foreach($data as $key => $value) {
            // if numeric key, assume array of rootNodeName elements
            if (is_numeric($key)) {
                $key = $rootNodeName;
            }

            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value)) {
                // create a new node unless this is an array of elements
                $node = Gio_Core_Array::isAssoc($value) ? $xml->addChild($key) : $xml;

                // recrusive call - pass $key as the new rootNodeName
                Gio_Core_Array::toXml($value, $key, $node);
            } else {
                // add single node.
                $value = utf8_encode($value);
                $node = $xml->addChild($key, $value);
            }

        }
        // pass back as string. or simple xml object if you want!
        return $xml->asXML();
    }

    // determine if a variable is an associative array
    public static function isAssoc($array) 
    {
        return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
    }
    
	public static function toObject($array) 
	{
		$object = new stdClass();
		foreach ($array as $key => $val) {
			$object->$key = is_array($val) ? self::toObject($val) : $val;
		}
		return $object;
	}
	
	public static function toIni($data, $section = false)
	{
 		$content = null;
		if ($section) {
			foreach ($data as $key => $elem) {
				$content .= "[" . $key ."]\n";
				foreach ($elem as $key2 => $elem2) {
					if(is_array($elem2)) {
						for($i=0; $i < count($elem2); $i++) {
							$content .= $key2."[] = \"".$elem2[$i]."\"\n";
						}
					} elseif($elem2 == '') {
						$content .= $key2." = \n";	
					} else {
						$content .= $key2 . " = \"" .$elem2 . "\"\n";
					}
				}
			}
		} else {
			foreach ($data as $key => $elem) {
				if(is_array($elem)) {
					for($i=0; $i < count($elem); $i++) {
						$content .= $key2 . "[] = \"" . $elem[$i] . "\"\n";
					}
				} elseif($elem == '') {
					$content .= $key2 . " = \n";
				} else {
					$content .= $key2 . " = \"" . $elem . "\"\n";
				}
			}
		}
		return $content;
	}
}
